package com.hmdp.service.impl;

import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopMapper;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@RequiredArgsConstructor
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    private final StringRedisTemplate stringRedisTemplate;
    private final ShopMapper shopMapper;

    @Override
    public List<ShopType> getList() {
        //1.在redis中查询
        String s = stringRedisTemplate.opsForValue().get(RedisConstants.SHOP_TYPE_LIST);
        //2.判断是否存在
        if(s != null) {
            //3.存在，直接返回
            return JSONUtil.toList(s, ShopType.class);
        }
        //4.不存在，查询数据库
        List<ShopType> sort = shopMapper.queryList();
        if(sort == null) {
            //5.数据库中不存在，返回错误
            return null;
        } else {
            //6.数据库中存在，写入redis
            stringRedisTemplate.opsForValue().set(RedisConstants.SHOP_TYPE_LIST, JSONUtil.toJsonStr(sort));
            //7.返回商户信息
            return sort;
        }
    }
}
